package com.renjibo.childred.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.renjibo.childred.R;
import com.renjibo.childred.adapter.MyAdapter;
import com.renjibo.childred.modle.TimeScareBean;
import com.renjibo.childred.servers.LongRunningServer;
import com.renjibo.childred.utils.PostHead;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 限时抢购的页面
 * A simple {@link Fragment} subclass.
 * Use the {@link ScareTimeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScareTimeFragment extends Fragment implements View.OnClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Button scare_button_return;
    private PullToRefreshListView scare_pullListView;
    private List<TimeScareBean.ProductlistBean> productArray=new ArrayList<>();
    private MyAdapter myAdapter;

    public ScareTimeFragment() {
        // Required empty public constructor
    }


    public static ScareTimeFragment newInstance(String param1, String param2) {
        ScareTimeFragment fragment = new ScareTimeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scare_time, container, false);
        initView(view);
        getUrlJson("/limitbuy");
        return view;
    }
    public void initView(View view){
        scare_button_return = (Button) view.findViewById(R.id.scare_button_return);
        scare_pullListView = (PullToRefreshListView) view.findViewById(R.id.scare_pullListView);
        scare_button_return.setOnClickListener(this);
    }
    public void getUrlJson(String url){
        RequestParams params = new RequestParams(PostHead.head+url);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                TimeScareBean timeScareBean = PostHead.gson.fromJson(result, TimeScareBean.class);
                List<TimeScareBean.ProductlistBean> productlist = timeScareBean.getProductlist();
                productArray.clear();
                productArray.addAll(productlist);
                Log.d("zzz","timeScareBean:"+timeScareBean);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                myAdapter = new MyAdapter(getActivity(),productArray);
                scare_pullListView.setAdapter(myAdapter);
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.scare_button_return://按钮点击  关闭
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
